﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GoodsInfoForm.aspx.cs"
    Inherits="PSS.WebSite.SetManage.BasicData.GoodsInfoForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>添加、编辑商品</title>
    <script src="../../lib/boot.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" method="post">
    <input name="GoodsId" class="mini-hidden" />
    <fieldset style="border: solid 1px #aaa; padding: 5px;">
        <legend>商品信息</legend>
        <table align="left" style="table-layout: fixed; width: 320;">
            <tr>
                <td style="width: 80px;" align="right">
                    商品编号：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="GoodsCode" class="mini-textbox" style="width: 300px;" required="true" />
                </td>
                <td style="width: 80px;" align="right">
                    商品名称：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="GoodsName" class="mini-textbox" style="width: 300px;" required="true" />
                </td>
            </tr>
            <tr>
                <td style="width: 80px;" align="right">
                    商品类别：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="GCategoryId" class="mini-combobox" style="width: 300px;" valuefield="GCategoryId"
                        textfield="GCategoryName" required="true" />
                </td>
                <td style="width: 80px;" align="right">
                    规格型号：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="Specific" class="mini-textbox" style="width: 300px;" />
                </td>
            </tr>
            <tr>
                <td style="width: 80px;" align="right">
                    首选仓库：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="StorageId" class="mini-combobox" style="width: 300px;" valuefield="StorageId"
                        textfield="StorageName" required="true" />
                </td>
                <td style="width: 80px;" align="right">
                    计量单位：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="UnitId" class="mini-combobox" style="width: 300px;" valuefield="UnitId"
                        textfield="UnitName" required="true" />
                </td>
            </tr>
            <tr>
                <td style="width: 80px;" align="right">
                    预计采购价：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="PurchasePrice" id="PurchasePrice" class="mini-spinner" style="width: 300px;"
                        minvalue="0" maxvalue="999999999" decimalplaces="4" minheight="100" />
                </td>
                <td style="width: 80px;" align="right">
                    预计销售价：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="SalePrice" id="SalePrice" class="mini-spinner" style="width: 300px;"
                        minvalue="0" maxvalue="999999999" decimalplaces="4" minheight="100" />
                </td>
            </tr>
        </table>
    </fieldset>
    <fieldset style="border: solid 1px #aaa; padding: 3px; height: 300px;">
        <legend>仓库信息</legend>
        <div class="mini-toolbar" style="padding: 2px; border-bottom: 0;">
            <table style="width: 100%;">
                <tr>
                    <td style="width: 100%;">
                        <a class="mini-button" iconcls="icon-add" onclick="addRow();" plain="true">添加</a>
                        <a class="mini-button" iconcls="icon-remove" onclick="removeRow()" plain="true">删除</a>
                    </td>
                </tr>
            </table>
        </div>
        <!--撑满页面-->
        <div class="mini-fit">
            <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;" idfield="DetailId"
                allowcelledit="true" allowcellselect="true" showfooter="false" ondrawcell="onDrawCell"
                oncellcommitedit="onCellCommitEdit" showsummaryrow="true" ondrawsummarycell="onDrawSummaryCell">
                <div property="columns">
                    <div type="checkcolumn" width="6%">
                    </div>
                    <div field="StorageId" width="100" displayfield="StorageName" headeralign="center"
                        allowsort="false">
                        仓库
                        <input property="editor" class="mini-combobox" style="width: 100%;" valuefield="StorageId"
                            textfield="StorageName" url="../../Service/SetManageHandler.ashx?action=GetStorageListByCombobox" />
                    </div>
                    <div field="InitialNumber" width="100" headeralign="center" allowsort="false">
                        期初数量
                        <input property="editor" class="mini-spinner" minvalue="0" maxvalue="999999999" decimalplaces="4"
                            style="width: 100%;" />
                    </div>
                    <div field="UnitCost" width="100" headeralign="center" allowsort="false">
                        单位成本
                        <input property="editor" class="mini-spinner" minvalue="0" maxvalue="999999999" decimalplaces="4"
                            style="width: 100%;" />
                    </div>
                    <div field="InitialPrice" width="80" headeralign="center" allowsort="false">
                        期初总价
                    </div>
                </div>
            </div>
        </div>
    </fieldset>
    <table style="width: 100%;">
        <tr>
            <td style="width: 12%;" align="right">
                备注：
            </td>
            <td style="width: 88%" align="left">
                <input name="Remark" class="mini-textarea" style="width: 100%; height: 50px;" />
            </td>
        </tr>
    </table>
    <div style="text-align: center; padding: 10px;">
        <a class="mini-button" iconcls="icon-save" onclick="onOk" style="width: 60px; margin-right: 20px;">
            保存</a> <a class="mini-button" iconcls="icon-cancel" onclick="onCancel" style="width: 60px;">
                取消</a>
    </div>
    </form>
    <script type="text/javascript">
        mini.parse();
        var GCategoryId = mini.getbyName("GCategoryId");
        var StorageId = mini.getbyName("StorageId");
        var UnitId = mini.getbyName("UnitId");
        GCategoryId.setUrl("../../Service/SetManageHandler.ashx?action=GetGoodsCategoryListByCombobox");
        StorageId.setUrl("../../Service/SetManageHandler.ashx?action=GetStorageListByCombobox");
        UnitId.setUrl("../../Service/SetManageHandler.ashx?action=GetUnitListByCombobox");
        var action;
        var grid = mini.get("datagrid1");
        var form = new mini.Form("form1");

        //提交单元格编辑数据前激发
        function onCellCommitEdit(e) {
            var editor = e.editor;
            editor.validate();
            if (editor.isValid() == false) {
                alert(editor.getErrorText());
                e.cancel = true;
            }
        }
        function onDrawCell(e) {
            var record = e.record;
            if (e.field == "InitialPrice") {
                var initialNumber = record.InitialNumber;
                var unitCost = record.UnitCost;
                e.cellHtml = (initialNumber * unitCost).toFixed(4);
            }
        }

        function onDrawSummaryCell(e) {
            var result = e.result;
            var grid = e.sender;
            var rows = e.data;
            if (e.field == "StorageId") {
                e.cellHtml = "总计:";
            }
            if (e.field == "InitialNumber") {
                var InitialNumberTotal = 0;
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    var t = row.InitialNumber;
                    if (isNaN(t)) continue;
                    InitialNumberTotal += t;
                }
                e.cellHtml = InitialNumberTotal;
            }
            if (e.field == "UnitCost") {
                var UnitCostTotal = 0;
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    var t = row.UnitCost;
                    if (isNaN(t)) continue;
                    UnitCostTotal += t;
                }
                e.cellHtml = UnitCostTotal;
            }
            if (e.field == "InitialPrice") {
                var InitialPriceTotal = 0;
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    var t = row.InitialPrice;
                    if (isNaN(t)) continue;
                    InitialPriceTotal += t;
                }
                e.cellHtml = InitialPriceTotal;
            }
        }

        function addRow() {
            var newRow = { name: "New Row" };
            grid.addRow(newRow, 0);
        }
        function removeRow() {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                grid.removeRows(rows, true);
            }
        }
        function SaveData() {
            var data = grid.getData();
            var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;
            if (data.length > 0) {
                var StorageIdCount = 0;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].StorageId == undefined) {
                        StorageIdCount = StorageIdCount + 1;
                    }
                }
                if (StorageIdCount > 0) {
                    mini.alert("仓库信息列表中有为空的字段，请重新选择...");
                }
                else {
                    saveData(o, data);
                }
            }
            else {
                mini.alert("请添加仓库信息！");
            }
        }
        function saveData(o, data) {
            o.arrList = data;
            var json = mini.encode([o]);
            if (viewaction == "edit") {
                $.ajax({
                    url: "../../Service/SetManageHandler.ashx?action=ModifyGoods",
                    data: { data: json },
                    type: 'POST',
                    success: function (text) {
                        CloseWindow("save");
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }
            else {
                $.ajax({
                    url: "../../Service/SetManageHandler.ashx?action=AddGoods",
                    data: { data: json },
                    type: 'POST',
                    success: function (text) {
                        CloseWindow("save");
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }
        }

        function SetData(data) {
            viewaction = data.action;
            if (data.action == "edit") {
                data = mini.clone(data);
                $.ajax({
                    url: "../../service/SetManageHandler.ashx?action=GetGoodsInfo&GoodsId=" + data.GoodsId,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);
                        $.ajax({
                            url: "../../service/SetManageHandler.ashx?action=GetGoodsDetailList&GoodsId=" + data.GoodsId,
                            cache: false,
                            success: function (text) {
                                var o = mini.decode(text);
                                grid.setData(o);
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                alert(jqXHR.responseText);
                            }
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });

            }
        }
        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "close" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                    return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }

        function onOk() {
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
    </script>
</body>
</html>
